﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using coffeeshop.coffeeDataSetTableAdapters;

namespace coffeeshop.Models
{
    public class Product
    {
        private static ProductTableAdapter _adapter = new ProductTableAdapter();

        public int ProductId { get; set; }
        public int ProductCategoryId { get; set; }
        public bool IsActive { get; set; }
        public int UnitId { get; set; }
        public int Price { get; set; }
        public int? CompoundProductId { get; set; }
        public string Name { get; set; }
        public string Code { get; set; }

        public Product() { }
        public Product(int productId) { ProductId = productId; }

        public static coffeeDataSet.ProductDataTable GetProductList(bool toHideInactive)
        {
            return _adapter.GetProductList(toHideInactive);
        }

        public static coffeeDataSet.ProductDataTable GetProductListWithIngredients(bool toHideInactive)
        {
            return _adapter.GetProductListWithIngredients(toHideInactive);
        }

        public static Product GetById(int id)
        {
            Product result = new Product();
            coffeeDataSet.ProductDataTable data = _adapter.GetById(id);

            if (data.Any())
            {
                result.ProductId = (int)data.Rows[0]["ProductId"];
                result.Name = (string)data.Rows[0]["Name"];
                result.Code = (string)(data.Rows[0]["Code"]);
                result.Price = (int)data.Rows[0]["Price"];
                result.UnitId = (int)data.Rows[0]["UnitId"];
                result.ProductCategoryId = (int)data.Rows[0]["ProductCategoryId"];

                if (data.Rows[0]["CompoundProductId"] == DBNull.Value)
                    result.CompoundProductId = null;
                else
                    result.CompoundProductId =(int)data.Rows[0]["CompoundProductId"];
            }

            return result;
        }

        public static Product GetByNameAndCode(string name, string code)
        {
            Product result = new Product();
            coffeeDataSet.ProductDataTable data = _adapter.GetByNameAndCode(name, code);

            if (data.Any())
            {
                result.ProductId = (int)data.Rows[0]["ProductId"];
                result.Name = (string)data.Rows[0]["Name"];
                result.Code = (string)(data.Rows[0]["Code"]);
                result.Price = (int)data.Rows[0]["Price"];
                result.UnitId = (int)data.Rows[0]["UnitId"];
                result.ProductCategoryId = (int)data.Rows[0]["ProductCategoryId"];

                if (data.Rows[0]["CompoundProductId"] == DBNull.Value)
                    result.CompoundProductId = null;
                else
                    result.CompoundProductId = (int)data.Rows[0]["CompoundProductId"];
            }

            return result;
        }

        public bool UpdateActiveness()
        {
            try
            {
                int result = _adapter.UpdateActiveness(IsActive, ProductId);

                return result == 1;
            }
            catch
            {
                return false;
            }
        }

        public bool Save()
        {
            try
            {
                if (ProductId == 0)
                {
                    coffeeDataSet.ProductDataTable productData = _adapter.AddProduct(Name, Code, UnitId, Price, ProductCategoryId);
                    ProductId = (int)productData.Rows[0]["ProductId"];
                }
                else
                {
                    int? compoundProductId = null;
                    if (CompoundProductId > 0)
                        compoundProductId = CompoundProductId;

                    _adapter.UpdateProduct(Name, Code, UnitId, Price, compoundProductId, ProductCategoryId, ProductId);
                }

                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
